/**
 * Created by Kpeng on 2017/7/3.
 * Depending on the jquery-1.12.4.min.js.
 * Depending on the common.js.
 * Depending on the style.min.css.
 * To cite this document, always state the source as shown above.
 * -------------------------------
 * Module:部门管理
 * -------------------------------
 */

$(function () {
    wzERP.Depart.init();
});

wzERP.Depart.init = function () {
    wzERP.Common.inlineDate('#rangestart', '#rangeend');
    wzERP.Depart.addHandle(); //添加部门
    wzERP.Depart.editHandle(); //编辑部门
    //wzERP.Depart.delHandle(); //删除部门
   // wzERP.Depart.turnPage();//分页
};

//添加部门
wzERP.Depart.addHandle = function () {
    var opt = {
        ele: '.addBtn',
        modalBox: '#modal',
        modalTitleStr: '添加部门',
        onApprove: function () {
            wzERP.Common.saveHandle('#department', '#departAddUrl', $(this).find('.setId'),"",wzERP.Common.delayReload(wzERP.delayTime));
        },
        snippets: this.url,
        callback: function () {
            this.type = this.event.data('type');
            if (this.type !== 'add') {
                var getIds = $('#modal').find('.setId').data('id');
                this.url = $("#departAddChildHtml").val() + "?id=" + getIds + "&type=" + this.type;
            } else {
                //添加部门要触发三级联动 维正集团是所有公司 其他是上级部门所属公司
                this.url = $("#departAddHtml").val();
            }
        }
    };
    wzERP.Common.handleData(opt);
};

//编辑部门
wzERP.Depart.editHandle = function () {
    var opt = {
        ele: '.editBtn',
        modalBox: '#modal',
        modalTitleStr: '编辑部门信息',
        onApprove: function () {
            wzERP.Common.saveHandle('#department', '#departUpdateUrl', $(this).find('.setId'),wzERP.Common.delayReload(wzERP.delayTime));
        },
        snippets: $("#departUpdateHtml").val()
        
    };
    wzERP.Common.handleData(opt);
};

//分页跳转
wzERP.Depart.turnPage = function () {
    var opt = {
        pageUrl: $("#departPageUrl").val(),
        pageSearch: "#searchForm",
        pagesTotal: parseInt($('#pages').val()),
        pageCurr: parseInt($('#current').val()),
        pageSize: parseInt($('#pageSize').val()),
        currId: "#current"
    };
    wzERP.Common.pagingHandle(opt);
};

//表单验证器
wzERP.Depart.verifier = function (_selector) {
    /*Arg Guide
     @_selector: 对象选择器
     */
    $(_selector).form({
        on: 'blur',
        inline: true,
        transition: 'fade',
        fields: {
            //部门名称
            departName: {
                identifier: 'departName',
                rules: [{
                    type: 'empty',
                    prompt: '请输入部门名称'
                },
                {
                    type: 'regExp[/^[^\\s]{1,18}$/]',
                    prompt: '请输入18位长度的部门名称'
                }]
            }
        }
    });
    
};
//公司部门联动
wzERP.Depart.changeScLevels = function (id) {
    var scLevel1 = $("#"+id).find('#depart'),
        getBusiNameList = $('#findCompanyListByParentDepart').val(),//获取上级部门公司接口
        busiName = $("#"+id).find('#cmp'),//上级部门公司列表
        recData = {};//接收的参数
    // 上级部门下拉事件
    console.info("上级部门下拉列表对象："+scLevel1);
    scLevel1.on('change', function () {
        var param = {"departParentId": $(this).val()};
        busiName.dropdown('clear');
        //渲染上级部门公司列表
        $.post(getBusiNameList, param, function (data) {
            var temp = '<option value=""></option>';//字符串拼接
            recData = data.data;
            $.each(recData, function () {
                var thisName = this.companyName,
                    thisVal = this.id;
                temp += '<option value=' + thisVal + '>' + thisName + '</option>'
            });
            busiName.empty().append(temp);
        });
    });
};



